<?php
/**
 * Created by PhpStorm.
 * User:毛文科
 * Date: 2019/6/19
 * Time: 17:16
 */
namespace shortmessage\bind;
use shortmessage\src\Configs;
use think\Exception;

class LInKai extends Configs{
    protected $result = '';
    public function getCorpID(){
        if(isset($this->corpid)){
            return $this->corpid;
        }
        throw new \Exception('没有设置corpid');
    }
    public function getPwd(){
        if(isset($this->pwd)){
            return $this->pwd;
        }
        throw new \Exception('没有设置pwd');
    }
    public function setMobile(string $mobil=''){
        if($mobil){
            $this->mobile = $mobil;
        }
    }
    public function getMobile(){
        if(isset($this->mobile)){
            return $this->mobile;
        }
        throw new \Exception('请设置手机');
    }
    public function send($Mobile='',$Content='',$SendTime=null,$post=1){
        if($Mobile){
            $this->setMobile($Mobile);
        }else{
            throw new Exception('请设置手机号码');
        }
        header("Content-type: text/html; charset=utf-8");
        date_default_timezone_set('PRC'); //设置默认时区为北京时间
        $url = "https://sdk2.028lk.com/sdk2/BatchSend2.aspx?";
        //$url = "http://sdk2.028lk.com:9880/sdk2/BatchSend2.aspx?";
        $ContentS = rawurlencode(mb_convert_encoding($Content, "gb2312", "utf-8"));//短信内容做GB2312转码处理
        $curpost = "CorpID=".$this->getCorpID()."&Pwd=".$this->getPwd()."&Mobile=".$this->getMobile()."&Content=".$Content."&SendTime=".$SendTime;
        if($post == 1)
        {
            //GET方式请求
            $ch = curl_init();
            //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查 -https
            //curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);
            curl_setopt($ch, CURLOPT_URL, $url.$curpost);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            $result = curl_exec($ch);
            curl_close($ch);
            //$result = file_get_contents($url.$curpost);
        }
        else
        {
            //POST方式请求
            $ch = curl_init();
            //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查 -https
            //curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $curpost);
            $result = curl_exec($ch);
            curl_close($ch);
        }
        $this->result = $result;
        return $this;
    }
    public function get(){
        return $this->result;
    }
    public function xml(){
        $xml = simplexml_load_string($this->result,'SimpleXMLElement',LIBXML_NOCDATA);
        $this->result  = (array)$xml;
        return $this;
    }
    public function jsonToArray(){
        $this->result = json_decode($this->result,true);
        return $this;
    }
}